package test.myclassloader;

public class Main {
	public static void main(String[] args) {

		try {
			MyURLClassLoader classLoader = new MyURLClassLoader();
			MyURLClassLoader classLoader2 = new MyURLClassLoader();
			Class classLoaded = classLoader.loadClass("test.myclassloader.MyClass");
			Class classLoaded2 = classLoader.loadClass("test.myclassloader.MyClass");
			Class classLoaded3 = classLoader2.loadClass("test.myclassloader.MyClass");			
			
			Object instance = classLoaded.newInstance();

			printClassLoader(classLoader);
			System.out.println();
			printClassLoader(classLoader2);
			System.out.println();
			
			System.out.println(classLoaded.hashCode());
			System.out.println(classLoaded2.hashCode());
			System.out.println(classLoaded3.hashCode());
			
			test.myclassloader.MyClass myClass = null;
			
			instance = null;
			classLoaded = null;
			classLoader = null;
			
			System.out.println("start to GC");
			System.gc();
			Thread.sleep(1000);
			System.gc();
			System.out.println("finish to GC");
			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	public static void printClassLoader(ClassLoader cldr){
		System.out.print(cldr);
		if(cldr.getParent() != null){
			System.out.print(" => ");
			printClassLoader(cldr.getParent());
		}
	}
}